package net.guoyixian.admin.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.guoyixian.admin.pojo.Permissions;
import net.guoyixian.admin.service.PermissionsService;
import net.guoyixian.common.web.GyxResult;

@Controller
@RequestMapping("permissions")
public class PermissionsController extends BaseController {

	@Autowired
	private PermissionsService permissionsService;

	@RequiresPermissions("permissions:query")
	@RequestMapping("list")
	public String list() {
		return "permissions/permissionslist";
	}

	@RequiresPermissions("permissions:query")
	@RequestMapping("query")
	@ResponseBody
	public String queryTreegrid(Integer id) {
		String treegrid = super.writeJson(permissionsService.queryTreegrid(id));
		return treegrid;
	}

	@RequiresPermissions("permissions:add")
	@RequestMapping("add")
	public String add() {
		return "permissions/permissionsadd";
	}

	@RequiresPermissions("permissions:add")
	@RequestMapping("save")
	@ResponseBody
	public GyxResult save(Permissions permissions) {
		permissionsService.save(permissions);
		return GyxResult.build(200, "新增成功");
	}

	/**
	 * 获得权限树
	 * 
	 * @param id
	 * @return
	 */
	@RequiresPermissions("permissions:query")
	@RequestMapping("querytree")
	@ResponseBody
	public String queryTree(Integer id) {
		return super.writeJson(permissionsService.tree(id, false));

	}

	@RequiresPermissions("permissions:query")
	@RequestMapping("querytreeRecursive")
	@ResponseBody
	public String queryTreeRecursive(Integer id) {
		return super.writeJson(permissionsService.tree(id, true));

	}

	@RequiresPermissions("permissions:delete")
	@RequestMapping("delete")
	@ResponseBody
	public GyxResult delete(Integer id) {
		permissionsService.delete(id);
		return GyxResult.build(200, "删除成功", id);
	}

	@RequiresPermissions("permissions:edit")
	@RequestMapping("editshow")
	public String editshow() {
		return "permissions/permissionsedit";
	}
	
	@RequiresPermissions("permissions:edit")
	@RequestMapping("edit")
	@ResponseBody
	public GyxResult edit(Permissions permissions) {
		permissionsService.save(permissions);
		return GyxResult.build(200, "修改成功");
	}
}